package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.BlogDao;
import dao.User;
import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
//    登陆或者注册
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        User user = null;
        UserDao userDao = new UserDao();
        String username=req.getParameter("username");
        String repassword=req.getParameter("passwords");//靠这个判断是注册还是登陆
        String password = req.getParameter("password");
        user = new User();
        user.setUsername(username);
        user.setPassword(password);

        if ( repassword != null) {
            //注册
            if(userDao.getUserByname(req.getParameter("username"))!=null ){
                resp.setContentType("application/json;charset=UTF-8");
                resp.getWriter().append("{\"massage\":\"2\"}");
                return;
            }
            //注册成功
            userDao.inserUser(user);
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().append("{\"massage\":\"1\"}");
            return;
        } else {
            // 登陆
            if (userDao.getUserByname(req.getParameter("username"))!=null && userDao.getUserByname(username).getPassword().equals(password)) {
                //创建会话
                user=userDao.getUserByname(username);
                HttpSession session = req.getSession(true);
                session.setAttribute("user", user);
                resp.setContentType("application/json;charset=UTF-8");
                resp.getWriter().append("{\"massage\":\"2\"}");
                return;
            }else{
                resp.setContentType("application/json;charset=UTF-8");
                resp.getWriter().append("{\"massage\":\"1\"}");
                return;
            }

        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 会话不存在, 就认为是未登录.
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 未登录
            resp.setStatus(403);
            return;
        }
        // 不仅仅是看 session 对象本身, 还需要看 user 对象存在. (为了后面实现 "退出登录" 功能)
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.setStatus(403);
            return;
        }
        // 返回 200 表示已经登陆.
        String respJson = objectMapper.writeValueAsString(user);
        resp.setContentType("application/json; charset=utf8");
        resp.getWriter().write(respJson);
        resp.setStatus(200);
    }
}
